package com.example.zbh.dsliao.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.easemob.chat.EMChatRoom;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.ui.activty.BaseActivity;

import java.util.List;

/**
 * Created by zbh on 15/12/1.
 */
public class ChatRoomAdapter extends BaseAdapter {
    private List<EMChatRoom> list;
    public void addData( List<EMChatRoom> list){
        this.list = list;
        notifyDataSetChanged();

    }


    public boolean isLength(){
        return list != null && list.size() > 0;
    }

    @Override
    public int getCount() {
        return isLength() ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.listview_chatroom_item, null);
        }else {
            view = convertView;
        }
        ViewHolder holder = ViewHolder.getHolder(view);
        if (isLength()){
        holder.chatRoomName.setText(list.get(position).getName());
        }

        return view;
    }
    public static class ViewHolder{
        TextView chatRoomName;
        public static ViewHolder getHolder(View view){
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null){
                holder = new ViewHolder();
                holder.chatRoomName = (TextView) view.findViewById(R.id.chatRoomName);
            }
            return holder;
        }
    }

}
